package GUI;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.OptionPaneUI;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JEditorPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.Window.Type;
import javax.swing.SpringLayout;
import Controller.ComplaintSubmissionController;
import Entity.Complaint;
import Entity.Complaint.validateErrs;
import javax.swing.JScrollPane;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * this is the GUI for submitting complaint. the customer will enter his details and will send the complaint.
 * @author Moran Abilea
 *
 */
public class SubmitComplaintBoundary extends JFrame {

	private JPanel contentPane;
	private JTextField CustomerID;
	private Complaint myGUIc=new Complaint(null,null); // just for having a new complaint object. 
	private boolean okflag=false; // this will be a flag that will indicates to save to the DB.
	
	/**
	 * is the constructor. it will validate the details and save it in the DB.
	 */
	public SubmitComplaintBoundary() {
		setTitle("Customer Complaint Window");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 491, 344);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblCustomerComplaintsSubmission = new JLabel("Customer Complaints Submission");
		lblCustomerComplaintsSubmission.setBounds(112, 21, 212, 14);
		lblCustomerComplaintsSubmission.setFont(new Font("Tahoma", Font.PLAIN, 14));
		contentPane.add(lblCustomerComplaintsSubmission);
		
		// the label of the id.
		JLabel lblEnterYourId = new JLabel("Enter your id here:");
		lblEnterYourId.setBounds(41, 46, 140, 14);
		contentPane.add(lblEnterYourId);
		CustomerID = new JTextField();
		// this part will block any possibility for trying to type an id with "undigit" chars.
		CustomerID.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent key) {
				char c=key.getKeyChar();
				if(!(Character.isDigit(c))||(c==KeyEvent.VK_BACK_SPACE)|| c==KeyEvent.VK_DELETE){
					getToolkit().beep();
					key.consume();
				}
			}
		});
		CustomerID.setBounds(148, 43, 100, 20);
		contentPane.add(CustomerID);
		CustomerID.setColumns(10);
		
		// the label of the description.
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(41, 140, 393, 79);
		contentPane.add(scrollPane);
		final JEditorPane ComplaintDesc = new JEditorPane();
		scrollPane.setViewportView(ComplaintDesc);
		JLabel lblTpeYourComplaint = new JLabel("Type your complaint Description here:");
		lblTpeYourComplaint.setBounds(41, 115, 369, 14);
		contentPane.add(lblTpeYourComplaint);
		
		
		
		JLabel lblComplaintNumber = new JLabel("Complaint number:"+myGUIc.getComplaint_number());
		lblComplaintNumber.setBounds(41, 72, 161, 14);
		contentPane.add(lblComplaintNumber);
		
		JLabel lblSubmissionDateAnd = new JLabel("Submission date and time:"+myGUIc.getDateOfsubmission());
		lblSubmissionDateAnd.setBounds(41, 90, 369, 14);
		contentPane.add(lblSubmissionDateAnd);
				
		JButton btnSubmitComplaint = new JButton("Submit complaint");
		btnSubmitComplaint.setBounds(30, 230, 161, 23);
		btnSubmitComplaint.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				myGUIc.setCustomer_id(CustomerID.getText());// getting the id text from the user.
				myGUIc.setDescription(" "+ComplaintDesc.getText());// getting the description text from the user.
				if(myGUIc.cvalidate()==validateErrs.IDERR){ // if the user entered less then 9 digits, throw  error pop up. 
					JOptionPane errid=new JOptionPane();
					String erridmsg="You entered an invalid id:"+CustomerID.getText()+"\nPlease enter a 9 digits id.";
					errid.setMessage(erridmsg);
					errid.setMessageType(JOptionPane.ERROR_MESSAGE);
					JDialog iddidalog=errid.createDialog(null,"Error! Wrong id.");
					iddidalog.setVisible(true);
				} 
				else if(myGUIc.cvalidate()==validateErrs.DESCERR){ // if the user didn't enter any description, throw  error pop up. 
					JOptionPane errid=new JOptionPane();
					String erridmsg="You didn't enter any description \n Please enter a description.";
					errid.setMessage(erridmsg);
					errid.setMessageType(JOptionPane.ERROR_MESSAGE);
					JDialog iddidalog=errid.createDialog(null,"Error! Needed Description.");
					iddidalog.setVisible(true);
				} 
				else if(myGUIc.cvalidate()==null){ // else, throw a good pop up.
					System.out.println(myGUIc.getCustomer_id());
					JOptionPane errid=new JOptionPane(); 
					String okidmsg="You finished entering your complaint. now sending...";
					errid.setMessage(okidmsg);
					errid.setMessageType(JOptionPane.INFORMATION_MESSAGE);
					JDialog iddidalog=errid.createDialog(null,"Sending...");
					iddidalog.setVisible(true);
					okflag=true;
				}
			}
		});
		
		// if everything went well, save to the DB.
		if(okflag)
			myGUIc.save();
		
		contentPane.add(btnSubmitComplaint);
		
		JButton btnCancel = new JButton("Cancel/Go back");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				close();
				CustomerGUI goback=new CustomerGUI();
				goback.setVisible(true);
			}
		});
		btnCancel.setBounds(268, 230, 166, 23);
		contentPane.add(btnCancel);
		
		JLabel lblIfYouWant = new JLabel("if you want to cancel");
		lblIfYouWant.setBounds(299, 253, 161, 14);
		contentPane.add(lblIfYouWant);
		
		JLabel lblOrIfYou = new JLabel("after saving, also click \"go back\"");
		lblOrIfYou.setBounds(30, 253, 196, 14);
		contentPane.add(lblOrIfYou);
	}
	
	public void close()
	{
		WindowEvent windowclose=new WindowEvent(this,WindowEvent.WINDOW_CLOSING);
		Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(windowclose);
	}
}